package cn.itsource.pet.service.impl;

import cn.itsource.basic.dto.PetDto;
import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.BaiduAIUtils;
import cn.itsource.basic.util.PageResult;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.PetDetail;
import cn.itsource.pet.domain.PetOnlineAuditLog;
import cn.itsource.pet.mapper.PetDetailMapper;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.mapper.PetOnlineAuditLogMapper;
import cn.itsource.pet.service.IPetService;
import cn.itsource.basic.service.impl.BaseImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class PetServiceImpl extends BaseImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;


    @Override
    public void delete(Long id) {
        super.delete(id);
        petDetailMapper.deleteByPetId(id);
    }


    @Override
    public void patchDelete(List<Long> ids) {
        super.patchDelete(ids);
        petDetailMapper.patchDeleteByPetId(ids);
    }


    @Override
    @Transactional
    public void update(PetDto petDto) {
        Pet pet = new Pet();
        BeanUtils.copyProperties(petDto, pet);
        petMapper.update(pet);


        PetDetail petDetail = new PetDetail();
        BeanUtils.copyProperties(petDto, petDetail);
        petDetailMapper.update(petDetail);

    }

    @Override
    @Transactional
    public void insert(PetDto petDto) {
        //构建Pet对象
        Pet pet = new Pet();
        BeanUtils.copyProperties(petDto, pet);
        //构建PetDetail对象
        PetDetail petDetail = new PetDetail();
        petDetail.setAdoptNotice(petDto.getAdoptNotice());
        petDetail.setIntro(petDto.getIntro());
        //先将Pet对象加入到数据库---》获得id
        petMapper.insert(pet);
        petDetail.setPetId(pet.getId());
        //将PetDetail对象加入到数据库
        petDetailMapper.insert(petDetail);
    }

    @Override
    @Transactional
    public void onShelves(List<Long> ids) {
        //通过ids查出所有的Pet对象
        List<Pet> pets = petMapper.selectByIds(ids);
        //遍历
        for (Pet pet : pets) {
            //百度AI审核name
            Boolean bool1 = BaiduAIUtils.textCensor(pet.getName());
            //批量添加审核日志
            PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
            petOnlineAuditLog.setPetId(pet.getId());
            petOnlineAuditLog.setAuditTime(new Date());
            if (bool1){
                petOnlineAuditLog.setState(1);
                petOnlineAuditLog.setNote("审核通过");
            }else {petOnlineAuditLog.setState(0);
                petOnlineAuditLog.setNote("审核失败");}

            petOnlineAuditLogMapper.insert(petOnlineAuditLog);
            if (!bool1) {
                throw new RuntimeException("宠物名字不合法");
            }
        }




        petMapper.onShelves(ids);
    }

    @Override
    public void offShelves(List<Long> ids) {
        petMapper.offShelves(ids);

    }

    @Override
    public PageResult<Pet> selectByQueryPage2(BaseQuery query) {
        Integer totals = petMapper.QueryCount2(query);
        List<Pet> list = (List<Pet>) petMapper.QueryData2(query);
        return new PageResult<Pet>(totals, list);
    }

}
